<template>
	<view class="order-index">
		<u-sticky>
			<u-subsection :list="orderStatus" :current="orderCurrent" @change="orderStatusChange" activeColor="#FF5722" :fontSize="28" mode="button"></u-subsection>
		</u-sticky>
		<view class="order-list">
			<u-empty
			    mode="order"
				:iconSize="100"
			    icon="order"
				:show="!orderList.length"
				:marginTop="100"
				:textSize="30"
			>
			</u-empty>
			
			<view class="order-item" v-for="(item,key) in orderList" :key="key">
				<view class="order-item__info flex">
					<view class="title">订单编号</view>
					<view class="value">{{ item.order_no }}</view>
				</view>
				<view class="order-item__info flex">
					<view class="title">商品名称</view>
					<view class="value">{{ item.goods_name }}</view>
				</view>
				<view class="order-item__info flex">
					<view class="title">兑换数量</view>
					<view class="value">
						<u-text :size="32" :text="item.nums" :bold="true"></u-text>
					</view>
				</view>
				<view class="order-item__info flex">
					<view class="title">实付金额</view>
					<view class="value flex">
						<view class="flex price-item" v-if="item.golds > 0">
							<text class="price">{{ item.golds }}</text>
							<text class="unit">金币</text>
						</view>
						
						<text class="plus" v-if="item.golds > 0 && item.order_amt > 0">+</text>
						
						<view class="flex price-item" v-if="item.order_amt > 0">
							<text class="price">{{ item.order_amt }}</text>
							<text class="unit">元</text>
						</view>
					</view>
				</view>
				<view class="order-item__info flex">
					<view class="title">兑换时间</view>
					<view class="value">
						<u-text :size="32" :text="item.create_time"></u-text>
					</view>
				</view>
				<view class="order-item__info flex">
					<view class="title">订单状态</view>
					<view class="value flex">
						<template v-if="item.pay_status==1">
							<u-text :size="32" color="#dd6161" :text="item.outer_status_display" :show="item.outer_status==0"></u-text>
							<u-text :size="32" color="#f29100" :text="item.outer_status_display" :show="item.outer_status==1"></u-text>
							<u-text :size="32" color="#18b566" :text="item.outer_status_display" :show="item.outer_status==2"></u-text>
							<u-text :size="32" color="#18b566" :text="item.outer_status_display" :show="item.outer_status==3"></u-text>

							<u-button text="确认收货" color="#18b566" size="mini" :customStyle="{width:'100rpx'}" v-if="item.outer_status==2" @click="confirmReciver(item.order_id)"></u-button>
						</template>
						
						<template v-else>
							<u-text :size="32" color="#dd6161" :text="item.outer_status_display"></u-text>
							
							<u-button text="去支付" color="#dd6161" size="mini" :customStyle="{width:'100rpx'}" v-if="item.outer_status==0" @click="payment(item.order_id)"></u-button>
						</template>
					</view>
				</view>
			</view>
		</view>
		
		<u-loadmore :status="loadStatus" fontSize="32" :iconSize="32" line />
	</view>
</template>

<script>
	import { getMallOrders,orderUnionPayment,mallOrderConfirmReciver } from '@/api/api.js'
	export default {
		data() {
			return {
				// 0未支付 1未发货 2已发货 3已收货
				orderStatus: [
					{name: '未支付', status: 0}, 
					{name: '未发货', status: 1}, 
					{name: '已发货', status: 2}, 
					{name: '已收货', status: 3},
				],
				loadStatus: 'loadmore',
				filter: {
					currentStatus: 0,
					page: 1
				},
				orderCurrent: 0,
				orderList: []
			}
		},
		methods:{
			orderStatusChange(index) {
				this.orderCurrent = index
				this.filter.currentStatus = this.orderStatus[this.orderCurrent].status
			},
			getOrders() {
				if ( this.loadStatus == 'nomore' ) {
					return console.log('nomore')
				}
				
				uni.showLoading({
					title: '加载中'
				})
				this.loadStatus = 'loading'
				getMallOrders(this.filter.currentStatus,this.filter.page).then(res => {
					uni.hideLoading()
					let _result = res.data || []
					if ( !_result.length ) {
						return this.loadStatus = 'nomore'
					}
					this.orderList = uni.$lv.array.union(this.orderList , _result)
					this.loadStatus = 'loadmore'
				})
			},
			confirmReciver(orderId) {
				uni.showModal({
					title: '操作提示',
					content: '确认收货吗？',
					success: function (res) {
						if (res.confirm) {
							mallOrderConfirmReciver(orderId).then(res=>{
								this.filter.currentStatus = 2
								return uni.showToast({
									title: "已确认收货",
								})
							})
						}
					}
				});
			},
			payment(orderId) {
				var _this = this
				orderUnionPayment(orderId).then(payres => {
					if ( payres.data.pay_status == 1 ) {
						_this.filter.currentStatus = 1
						return uni.showToast({
							title: "支付成功~",
						})
					}
					
					if ( payres.data.pay_params ) {
						uni.requestPayment({
							...payres.data.pay_params,
							success: () => {
								_this.filter.currentStatus = 1
								return uni.showToast({
									title: "支付成功~",
								})
							},
							fail: () => {
								return uni.showToast({
									title: "支付失败~",
									icon: 'error'
								})
							}
						})
					}
				})
			}
		},
		onLoad: function (option) { 
			this.orderCurrent = parseInt(option.current)
		},
		mounted() {
			this.getOrders()
		},
		/**
		 * 下拉刷新回调函数
		 */
		onPullDownRefresh() {
			this.loadStatus = 'loadmore'
			this.filter.page = 1
			this.filter.currentStatus = ''
			this.orderList = []
			this.getOrders()
			uni.stopPullDownRefresh()
		},
		/**
		 * 上拉加载回调函数
		 */
		onReachBottom() {
			this.filter.page++
			this.getOrders()
		},
		watch: {
			'filter.currentStatus': function(newVal , oldVal) {
				this.loadStatus = 'loadmore'
				this.orderList = []
				this.getOrders()
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: $uni-bg-color-grey;
	}
	.order-index {
		padding-bottom: 30rpx;
		.order-list {
			padding: 30rpx;
			.order-item {
				padding: 30rpx;
				background-color: #FFFFFF;
				border-radius: 20rpx;
				&__info {
					.title{
						font-size: 30rpx;
						margin-right: 20rpx;
						color: $uni-text-color-placeholder;
						width: 130rpx;
						@include flex;
						align-items: center;
					}
					.value {
						font-size: 32rpx;
						color: #333;
						flex: 1;
					}
				}
				&__info:not(:last-child){margin-bottom: 20rpx;}
			}
			.order-item:not(:last-child){margin-bottom: 40rpx;}
		}
	}
</style>
